package com.literature.search.file.config;

import com.literature.search.file.task.quartz.ScheduledTask;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import java.util.Date;

/**
 * @ClassName QuartzConfiguration
 * @Description TODO
 * @Author abin
 * @Date 2023/3/24 16:23
 * @Version 1.0
 */
@Configuration
public class QuartzConfiguration {

    // 配置定时任务1的任务实例
    @Bean(name = "likeAndCollectionRecords2DBDetail")
    public MethodInvokingJobDetailFactoryBean likeAndCollectionRecords2DBDetail(ScheduledTask scheduledTask) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(true);
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(scheduledTask);
        // 需要执行的方法
        jobDetail.setTargetMethod("likeAndCollectionRecords2DB");
        return jobDetail;
    }


    // 配置触发器1
    @Bean(name = "likeAndCollectionRecords2DBTrigger")
    public SimpleTriggerFactoryBean likeAndCollectionRecords2DBTrigger(JobDetail likeAndCollectionRecords2DBDetail) {
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(likeAndCollectionRecords2DBDetail);
        // 设置任务启动延迟
        trigger.setStartDelay(0);
        // 设置定时任务启动时间
        trigger.setStartTime(new Date());
        // 每30秒执行一次
        trigger.setRepeatInterval(30000);
        return trigger;
    }

    @Bean(name = "updateFile2DB2ESDetail")
    public MethodInvokingJobDetailFactoryBean updateFile2DB2ESDetail(ScheduledTask scheduledTask) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(true);
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(scheduledTask);
        // 需要执行的方法
        jobDetail.setTargetMethod("updateFile2DB2ES");
        return jobDetail;
    }


    @Bean(name = "updateFile2DB2ESTrigger")
    public SimpleTriggerFactoryBean updateViewAndDownloadCountTrigger(JobDetail updateFile2DB2ESDetail) {
        SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
        trigger.setJobDetail(updateFile2DB2ESDetail);
        // 设置任务启动延迟
        trigger.setStartDelay(0);
        // 设置定时任务启动时间
        trigger.setStartTime(new Date());
        // 每30秒执行一次
        trigger.setRepeatInterval(60000);
        return trigger;
    }

    @Bean(name = "searchHotWord2DBDetail")
    public MethodInvokingJobDetailFactoryBean searchHotWord2DBDetail(ScheduledTask scheduledTask) {
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
        // 是否并发执行
        jobDetail.setConcurrent(true);
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(scheduledTask);
        // 需要执行的方法
        jobDetail.setTargetMethod("SearchHotWord2DB");
        return jobDetail;
    }


    @Bean(name = "searchHotWord2DBTrigger")
    public CronTriggerFactoryBean searchHotWord2DBTrigger(JobDetail searchHotWord2DBDetail) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(searchHotWord2DBDetail);
        // 设置任务启动延迟
        trigger.setStartDelay(0);
        // 设置定时任务启动时间
        trigger.setStartTime(new Date());
        //每天23点55开始执行
        trigger.setCronExpression("0 55 23 * * ? ");
        return trigger;
    }

    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger likeAndCollectionRecords2DBTrigger,Trigger updateFile2DB2ESTrigger,Trigger searchHotWord2DBTrigger){
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setStartupDelay(1);
        bean.setTriggers(likeAndCollectionRecords2DBTrigger,updateFile2DB2ESTrigger,searchHotWord2DBTrigger);
        return bean;
    }
}
